package jwlee.testfile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class FileReader {
	private static String path = System.getProperty("user.dir")
			+ "\\src\\jwlee\\testfile\\";

	public static void main(String[] args) throws Exception {
		String s1 = read("1.txt", "utf-8");
		System.out.println(s1);
		String s2 = read(path + "table.htm", "gbk");
		System.out.println(s2);
	}

	/**
	 * 
	 * @param path
	 * @param charset
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws FileNotFoundException
	 * @throws IOException
	 */
	public static String read(String path, String charset) throws IOException {
		if (charset == null)
			charset = "utf-8";
		BufferedReader br = new BufferedReader(new InputStreamReader(
				new FileInputStream(new File(path)), charset));
		StringBuffer sb = new StringBuffer();
		String perLine = null;
		while ((perLine = br.readLine()) != null) {
			sb.append(perLine).append("\n");
		}
		return sb.toString();
	}
}
